// pages/detail/appoint-detail.js
import routerManager from "../../utils/routerManager.js";
import http from "../../utils/http";
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fortuneteller: {},
    fortunetellerId: null,
    showPopup: false,
    hasUserInfo:false,
  },
  toIntroduce(event) {
    let service = event.currentTarget.dataset.service;
    routerManager.toIntroduce(service,this.data.fortuneteller);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    let fid;
    console.log("onload",options);
    if(options.scene){
      let q = decodeURIComponent(options.scene);
      fid = q.split("=")[1];
      console.log("onload fid=",fid);
    } else {
      fid = options.fid;
    }
    that.setData({
      fortunetellerId: fid
    });
    wx.getSetting({
      success(res) {
        let hasUserInfo = res.authSetting["scope.userInfo"];
        if(hasUserInfo){
          that.setData({
            hasUserInfo:true
          });
        }
      }
    });
  },
  share: function(event) {
    this.setData({
      showPopup: true
    });
  },
  savePic: function() {
    let cid = app.globalData.userInfo.id;
    let serviceId = this.data.fortuneteller.services[0].id;
    let fid =  this.data.fortuneteller.id;
    let path = "/pages/detail/fortuneteller-detail";
    let module = "fortuneteller";
    routerManager.toWebview(cid,serviceId,path,module,fid);
  },
  userInfoCallback: function(event) {
    let that = this;
    http.oauthLogin(event, data => {
      that.setData({
        hasUserInfo:true
      });
      http.fortuneteller(that.data.fortunetellerId, function(res) {
        if (res.error == 0) {
          that.setData({
            fortuneteller: res.data
          });
          if(!res.data.is_followed){
            that.onFollow();
          }
        }
      });
    });
  },
  onFollow: function(event) {
    let that = this;
    http.follow(that.data.fortunetellerId, function(res) {
      let fortuneteller = that.data.fortuneteller;
      fortuneteller.is_followed = true;
      that.setData({
        fortuneteller: fortuneteller
      });
      wx.showToast({
        title: "关注成功!"
      });
    });
  },
  onUnfollow: function(event) {
    let that = this;
    http.unfollow(that.data.fortunetellerId, function(res) {
      let fortuneteller = that.data.fortuneteller;
      fortuneteller.is_followed = false;
      that.setData({
        fortuneteller: fortuneteller
      });
      wx.showToast({
        title: "取消关注成功!"
      });
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let that = this;
    http.fortuneteller(that.data.fortunetellerId, function(res) {
      if (res.error == 0) {
        console.log("token",wx.getStorageSync("token"))
        console.log("fortuneteller", res.data);
        that.setData({
          fortuneteller: res.data
        });
        wx.setNavigationBarTitle({
          title: res.data.nickname
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    console.log("onShareAppMessage");
    return {
      title: this.data.fortuneteller.nickname,
      desc: this.data.fortuneteller.description,
      imageUrl: this.data.fortuneteller.image
    };
  }
});
